<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="供客名称" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供客名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供客类型" prop="isType">
        <el-select
          v-model="queryParams.isType"
          placeholder="请选择供客类型"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.supermarket_supply"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="isSh">
        <el-select
          placeholder="请选择类型"
          v-model="queryParams.isSh"
          @keyup.enter.native="handleQuery"
        >
          <el-option label="已通过" value="0" />
          <el-option label="待审核" value="1" />
          <el-option label="未通过" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="supplyList">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        label="供客编号"
        align="center"
        prop="supplyCode"
        width="180"
      />
      <el-table-column
        label="供客名称"
        align="center"
        prop="supplyName"
        width="180"
      />
      <el-table-column
        label="供客负责人"
        align="center"
        prop="supplyContact"
        width="100"
      />
      <el-table-column
        label="供客电话"
        align="center"
        prop="supplyPhone"
        width="120"
      />
      <el-table-column
        label="供客地址"
        align="center"
        prop="supplierAddress"
        width="250"
      />
      <el-table-column
        label="供客类型"
        align="center"
        prop="isType"
        width="150"
      >
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.isType === '1'">供应商</el-tag>
          <el-tag type="success" v-if="scope.row.isType === '2'">客户</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isSh" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isSh === '0'">已通过</el-tag>
          <el-tag type="info" v-if="scope.row.isSh === '1'">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.isSh === '2'">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === '0'">启用</el-tag>
          <el-tag type="danger" v-if="scope.row.status === '1'">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        prop="createName"
        width="100"
      />
      <el-table-column label="备注" align="center" prop="remark" width="150" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="68"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '1'"
            :disabled="scope.row.isSh !== '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQj(scope.row)"
            v-hasPermi="['supermarket:supply:qj']"
            >启用</el-button
          >
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQj(scope.row)"
            v-hasPermi="['supermarket:supply:qj']"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listSupply ,qjSupply} from "@/api/supermarket/supply";

export default {
  name: "Supply",
  dicts: ["sys_normal_disable", "supermarket_supply"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供客表格数据
      supplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplyCode: null,
        supplyName: null,
        supplyContact: null,
        supplyPhone: null,
        isType: null,
        status: null,
        isSh: null,
        createName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplyName: [
          { required: true, message: "供客名称不能为空", trigger: "blur" },
        ],
        supplyContact: [
          { required: true, message: "供客负责人不能为空", trigger: "blur" },
        ],
        supplyPhone: [
          { required: true, message: "供客电话不能为空", trigger: "blur" },
        ],
        isType: [
          { required: true, message: "供客类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供客列表 */
    getList() {
      this.loading = true;
      const userName = this.$store.state.user.name;
      this.queryParams.createName = userName;
      listSupply(this.queryParams).then((response) => {
        this.supplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplyId: null,
        supplyCode: null,
        supplyName: null,
        supplyContact: null,
        supplyPhone: null,
        supplierAddress: null,
        isType: null,
        status: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createName: null,
        updateName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQj(row) {
      qjSupply(row.supplyId).then(res => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      })
    }
  },
};
</script>
